We are migrating the bug tracker to github Issues. This is now the preferred way to report NASM bugs.

Self-registration is disabled due to spam issue (mail gorcunov@gmail.com or hpa@zytor.com to create an account)

Bug 3392747 - git nasm: bitshift left by 1 (<< 1) in %assign appears to shift right
Summary: git nasm: bitshift left by 1 (<< 1) in %assign appears to shift right
Status: CLOSED FIXED
Alias: None
Product: NASM
Classification: Unclassified
Component: Assembler (show other bugs)
Version: 2.16.xx
Hardware: All All
: Medium critical
Assignee: nobody
URL:
Depends on:
Blocks:
 
Reported: 2021-03-24 12:53 PDT by E. C. Masloch
Modified: 2022-02-18 05:04 PST (History)
6 users (show)

Obtained from: Built from git using configure
Generated by: ---
Bug category:
Observed for: ---
Regression: ---
Regression since:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description E. C. Masloch 2021-03-24 12:53:16 PDT
$ (cd ~/proj/nasm/; git describe)
nasm-2.15.05-118-g5368e457
$ newnasm -v
NASM version 2.16rc0 compiled on Mar 24 2021
$ cat test2.asm

%idefine words(b)       ((b)+1>>1)
%idefine fromwords(w)           ((w)<<1)

LOADCMDLINE_size equ 256
%assign ll fromwords(words(LOADCMDLINE_size))
%assign mm LOADCMDLINE_size
%assign nn (words(LOADCMDLINE_size))
%assign oo fromwords(nn)

dw ll           ; expected 0100h
dw mm           ; expected 0100h
dw nn           ; expected 0080h
dw oo           ; expected 0100h
$ newnasm test2.asm -l /dev/stderr
     1
     2                                  %idefine words(b)       ((b)+1>>1)
     3                                  %idefine fromwords(w)           ((w)<<1)
     4
     5                                  LOADCMDLINE_size equ 256
     6                                  %assign ll fromwords(words(LOADCMDLINE_size))
     7                                  %assign mm LOADCMDLINE_size
     8                                  %assign nn (words(LOADCMDLINE_size))
     9                                  %assign oo fromwords(nn)
    10
    11 00000000 4000                    dw ll           ; expected 0100h
    12 00000002 0001                    dw mm           ; expected 0100h
    13 00000004 8000                    dw nn           ; expected 0080h
    14 00000006 4000                    dw oo           ; expected 0100h
$
Comment 1 E. C. Masloch 2021-03-24 12:54:20 PDT
equ appears to work as expected.
Comment 2 E. C. Masloch 2021-03-24 13:11:40 PDT
Same test case except using equ, which works fine:


$ perl -pe 's/^\%assign\s+(\S+)\s+/$1 equ /' test2.asm > test3.asm
$ cat test3.asm

%idefine words(b)       ((b)+1>>1)
%idefine fromwords(w)           ((w)<<1)

LOADCMDLINE_size equ 256
ll equ fromwords(words(LOADCMDLINE_size))
mm equ LOADCMDLINE_size
nn equ (words(LOADCMDLINE_size))
oo equ fromwords(nn)

dw ll           ; expected 0100h
dw mm           ; expected 0100h
dw nn           ; expected 0080h
dw oo           ; expected 0100h
$ newnasm test3.asm -l /dev/stderr
     1
     2                                  %idefine words(b)       ((b)+1>>1)
     3                                  %idefine fromwords(w)           ((w)<<1)
     4
     5                                  LOADCMDLINE_size equ 256
     6                                  ll equ fromwords(words(LOADCMDLINE_size))
     7                                  mm equ LOADCMDLINE_size
     8                                  nn equ (words(LOADCMDLINE_size))
     9                                  oo equ fromwords(nn)
    10
    11 00000000 0001                    dw ll           ; expected 0100h
    12 00000002 0001                    dw mm           ; expected 0100h
    13 00000004 8000                    dw nn           ; expected 0080h
    14 00000006 0001                    dw oo           ; expected 0100h
$
Comment 3 H. Peter Anvin 2021-03-24 13:21:23 PDT
I suspect << and >> are mistokenizer in the preprocessor.
Comment 4 E. C. Masloch 2021-03-24 13:36:09 PDT
https://github.com/netwide-assembler/nasm/blob/5368e4579403daaf6c12165eb857893f8acfc7f8/asm/preproc.c#L1613

This is the culprit. It should set TOKEN_SHL but says TOKEN_SHR.
Comment 5 E. C. Masloch 2021-03-24 13:43:02 PDT
With this patch NASM appears to work. Building lDDebug symbolic results in exactly the same binary as with NASM version 2.15.03 which I used until now.

nasm$ git diff
diff --git a/asm/preproc.c b/asm/preproc.c
index d1b9b31b..cb1669da 100644
--- a/asm/preproc.c
+++ b/asm/preproc.c
@@ -1613,7 +1613,7 @@ static Token *tokenize(const char *line)
            case '<':
                if (*p == '<') {
                    p++;
-                    type = TOKEN_SHR;
+                    type = TOKEN_SHL;
                    if (*p == '<')
                        p++;
                } else if (*p == '=') {
nasm$
Comment 6 E. C. Masloch 2021-03-24 14:06:37 PDT
I checked out the commit that contained this error. I didn't see any other obvious error. It's at https://github.com/netwide-assembler/nasm/commit/20e0d616dc954d567c8bf2c7e11cc5d6c10ac544
Comment 7 Krister Nordvall 2022-02-18 05:04:48 PST
Oleg Oshmyan apparently fixed this 5 months ago so why is it not closed?

I'm closing this now.